using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;

namespace MacomberMap.Common.Database_Interface
{
    /// <summary>
    /// This class handles email sending
    /// </summary>
    public class MM_Email_Sending
    {
        /// <summary>
        /// Send an email 
        /// </summary>
        /// <param name="SMTPServer">The SMTP server</param>
        /// <param name="FromAddress"></param>
        /// <param name="FromName"></param>
        /// <param name="Subject"></param>
        /// <param name="Recipients"></param>
        /// <param name="CarbonCopies"></param>
        /// <param name="BlindCarbonCopies"></param>
        /// <param name="Message"></param>
        /// <param name="Attachments"></param>
        public static void SendEmail(String SMTPServer, String FromAddress, String FromName, String Subject, IEnumerable<String> Recipients, IEnumerable<String> CarbonCopies, IEnumerable<String> BlindCarbonCopies, String Message, params String[] Attachments)
        {
            //Create our SMTP serssion and message
            SmtpClient Session = new SmtpClient(SMTPServer);
            MailMessage OutMessage = new MailMessage();

            //Set our from, subject & recipients
            OutMessage.From = new MailAddress(FromAddress, FromName);
            foreach (String Recipient in Recipients)
                OutMessage.To.Add(new MailAddress(Recipient));
            foreach (String CarbonCopy in CarbonCopies)
                if (!String.IsNullOrEmpty(CarbonCopy))
                    OutMessage.CC.Add(new MailAddress(CarbonCopy));
            foreach (String BlindCarbonCopy in BlindCarbonCopies)
                if (!String.IsNullOrEmpty(BlindCarbonCopy))
                    OutMessage.CC.Add(new MailAddress(BlindCarbonCopy));
            
            //Set the subject and body
            OutMessage.Subject = Subject;
            OutMessage.IsBodyHtml = false;
            OutMessage.Body = Message;

            try
            {
                foreach (String str in Attachments)
                    OutMessage.Attachments.Add(new Attachment(str));
            }
            catch (Exception ex)
            {
                //File.WriteAllText("Error-processing-Attachments-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log", ex.Message.ToString() + "\n" + ex.InnerException.Message.ToString() + "\n" + ex.InnerException.StackTrace.ToString());
                Console.WriteLine("Errorprocessing Attachments: " + ex.Message + "\n" + ex.InnerException.Message + "\n" + ex.InnerException.StackTrace);
            }

            //Configure the SMTP session, and send out the message
            Session.ServicePoint.MaxIdleTime = 1000;
            Session.SendCompleted += new SendCompletedEventHandler(Session_SendCompleted);
            Session.SendAsync(OutMessage, Message);
        }

        /// <summary>
        /// Handle the completion of the message
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Session_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
         //   (e.UserState as MailMessage).Dispose();
        }

    }
}
